SlideShare a Scribd company logo
Class No.27  Data Structures http://ecomputernotes.com
Priority Queue Using Heap #include “Event.cpp” #include “Heap.cpp” #define PQMAX 30 class PriorityQueue  { public: PriorityQueue() { heap = new Heap<Event>( PQMAX ); }; ~PriorityQueue() { delete heap; }; http://ecomputernotes.com
Priority Queue Using Heap Event* remove() { if( !heap->isEmpty() ) { Event* e;  heap->deleteMin( e ); return e; } return (Event*)NULL; cout << &quot;remove - queue is empty.&quot; << endl; }; http://ecomputernotes.com
Priority Queue Using Heap int insert(Event* e)  { if( !heap->isFull() ) { heap->insert( e ); return 1; } cout << &quot;insert queue is full.&quot; << endl; return 0; }; int full(void){ return heap->isFull(); }; int length() { return heap->getSize(); }; };  http://ecomputernotes.com
The Selection Problem Given a list of  N  elements (numbers, names etc.), which can be totally ordered, and an integer  k , find the  k th smallest (or largest) element. One way is to put these  N  elements in an array an sort it. The  k th  smallest of these is at the  k th  position. http://ecomputernotes.com
The Selection Problem A faster way is to put the  N  elements into an array and apply the  buildHeap  algorithm on this array. Finally, we perform  k deleteMin  operations. The last element extracted from the heap is our answer. The interesting case is  k  =  N /2, since this is known as the  median . http://ecomputernotes.com
HeapSort If  k  =  N , and we record the  deleteMin  elements as they come off the heap, we will have essentially sorted the  N  elements. Later in the course, we will refine this idea to obtain a fast sorting algorithm called  heapsort . http://ecomputernotes.com
Disjoint Set ADT Suppose we have a database of people. We want to figure out who is related to whom. Initially, we only have a list of people, and information about relations is gained by updates of the form “Haaris is related to Saad”. http://ecomputernotes.com
Disjoint Set ADT Key property: If Haaris is related to Saad and Saad is related to Ahmad, then Haaris is related to Ahmad. Once we have relationships information, we would like to answer queries like “Is Haaris related to Ahmad?” http://ecomputernotes.com
Disjoint Set ADT Blob Coloring A well-known low-level computer vision problem for black and white images is the following: Gather together all the picture elements (pixels) that belong to the same &quot;blobs&quot;, and give each pixel in each different blob an identical label.  http://ecomputernotes.com
Disjoint Set ADT Blob Coloring Thus in the following image, there are five blobs.  We want to  partition  the pixels into  disjoint sets , one set per blob. http://ecomputernotes.com
Disjoint Set ADT The image segmentation problem. http://ecomputernotes.com
Equivalence Relations A binary relation  over a set S is called an  equivalence relation  if it has following properties Reflexivity: for all element xS, x  x Symmetry: for all elements x and y, x  y if and only if y  x Transitivity: for all elements x, y and z, if x  y and y  z then x  z The relation “is related to” is an equivalence relation over the set of people http://ecomputernotes.com
Equivalence Relations The  relationship is  not  an equivalence relation.  It is reflexive, since x  x,  and transitive, since x  y and y  z  implies x  z,  it is  not symmetric  since x  y does not imply y  x. http://ecomputernotes.com

More Related Content

PDF
Echo Function
PPTX
130717666736980000
PPT
Sets and disjoint sets union123
PPT
computer notes - Data Structures - 35
PPSX
Data structure stack&queue basics
PPT
computer notes - Data Structures - 8
PPT
computer notes - Data Structures - 26
PPT
computer notes - Data Structures - 29
Echo Function
130717666736980000
Sets and disjoint sets union123
computer notes - Data Structures - 35
Data structure stack&queue basics
computer notes - Data Structures - 8
computer notes - Data Structures - 26
computer notes - Data Structures - 29

Viewers also liked (20)

PPT
computer notes - Data Structures - 1
PPT
computer notes - Data Structures - 18
PPT
computer notes - Data Structures - 10
PPT
computer notes - Data Structures - 3
PPT
computer notes - Data Structures - 15
PPT
computer notes - Data Structures - 13
PPT
computer notes - Data Structures - 14
PPT
computer notes - Data Structures - 4
PPT
computer notes - Data Structures - 12
PPT
computer notes - Data Structures - 20
PPT
computer notes - Data Structures - 9
PPT
computer notes - Data Structures - 38
PPT
computer notes - Data Structures - 11
PPT
computer notes - Data Structures - 6
PPT
computer notes - Data Structures - 23
PPT
computer notes - Data Structures - 28
PPT
computer notes - Data Structures - 2
PPT
computer notes - Data Structures - 25
PDF
computer notes - Deleting a node
PPT
computer notes - Data Structures - 33
computer notes - Data Structures - 1
computer notes - Data Structures - 18
computer notes - Data Structures - 10
computer notes - Data Structures - 3
computer notes - Data Structures - 15
computer notes - Data Structures - 13
computer notes - Data Structures - 14
computer notes - Data Structures - 4
computer notes - Data Structures - 12
computer notes - Data Structures - 20
computer notes - Data Structures - 9
computer notes - Data Structures - 38
computer notes - Data Structures - 11
computer notes - Data Structures - 6
computer notes - Data Structures - 23
computer notes - Data Structures - 28
computer notes - Data Structures - 2
computer notes - Data Structures - 25
computer notes - Deleting a node
computer notes - Data Structures - 33
Ad

Similar to computer notes - Data Structures - 27 (20)

PDF
Unit-I-DAA.pdf
PPT
PPT
Temporal PPT details about the platform and its uses
PDF
(slides 1) Visual Computing: Geometry, Graphics, and Vision
PDF
Advanced Algorithms Lecture Notes Mit 6854j Itebooks
PDF
Iare ds lecture_notes_2
PDF
lecture4.pdf
PPTX
DATA STRUCTURE ooooooooooooooooooooooooaaaaaaappt.pptx
PDF
Introduction To Algorithms 4th Thomas H Cormen Charles E Leiserson
DOCX
Algorithm
PPT
lecture 11
PPT
Mid termexam review
PPS
Data Structure
PPS
Lec 1 Ds
PPS
Lec 1 Ds
PDF
Introductionofdatastructure 110731092019-phpapp01
PDF
Introduction of data_structure
PPTX
Algorithms__Data_Structures_-_iCSC_2018.pptx
PPT
pixelrelationships-m-1.pptx.ppt
PPTX
DA_02_algorithms.pptx
Unit-I-DAA.pdf
Temporal PPT details about the platform and its uses
(slides 1) Visual Computing: Geometry, Graphics, and Vision
Advanced Algorithms Lecture Notes Mit 6854j Itebooks
Iare ds lecture_notes_2
lecture4.pdf
DATA STRUCTURE ooooooooooooooooooooooooaaaaaaappt.pptx
Introduction To Algorithms 4th Thomas H Cormen Charles E Leiserson
Algorithm
lecture 11
Mid termexam review
Data Structure
Lec 1 Ds
Lec 1 Ds
Introductionofdatastructure 110731092019-phpapp01
Introduction of data_structure
Algorithms__Data_Structures_-_iCSC_2018.pptx
pixelrelationships-m-1.pptx.ppt
DA_02_algorithms.pptx
Ad

More from ecomputernotes (19)

PPT
computer notes - Data Structures - 30
PPT
computer notes - Data Structures - 39
DOC
Computer notes - Including Constraints
DOC
Computer notes - Date time Functions
DOC
Computer notes - Subqueries
DOC
Computer notes - Other Database Objects
PPT
computer notes - Data Structures - 19
PPT
computer notes - Data Structures - 31
DOC
Computer notes - Advanced Subqueries
DOC
Computer notes - Aggregating Data Using Group Functions
PPT
computer notes - Data Structures - 16
PPT
computer notes - Data Structures - 22
PPT
computer notes - Data Structures - 36
DOC
Computer notes - Enhancements to the GROUP BY Clause
DOC
Computer notes - Manipulating Data
DOC
Computer notes - Writing Basic SQL SELECT Statements
PPT
computer notes - Data Structures - 5
DOC
Computer notes - Controlling User Access
DOC
Computer notes - Using SET Operator
computer notes - Data Structures - 30
computer notes - Data Structures - 39
Computer notes - Including Constraints
Computer notes - Date time Functions
Computer notes - Subqueries
Computer notes - Other Database Objects
computer notes - Data Structures - 19
computer notes - Data Structures - 31
Computer notes - Advanced Subqueries
Computer notes - Aggregating Data Using Group Functions
computer notes - Data Structures - 16
computer notes - Data Structures - 22
computer notes - Data Structures - 36
Computer notes - Enhancements to the GROUP BY Clause
Computer notes - Manipulating Data
Computer notes - Writing Basic SQL SELECT Statements
computer notes - Data Structures - 5
Computer notes - Controlling User Access
Computer notes - Using SET Operator

Recently uploaded (20)

PDF
Electronic commerce courselecture one. Pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Approach and Philosophy of On baking technology
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PPTX
sap open course for s4hana steps from ECC to s4
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PPTX
Big Data Technologies - Introduction.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Machine learning based COVID-19 study performance prediction
PDF
Review of recent advances in non-invasive hemoglobin estimation
PDF
Encapsulation theory and applications.pdf
PPT
Teaching material agriculture food technology
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
Electronic commerce courselecture one. Pdf
Unlocking AI with Model Context Protocol (MCP)
Dropbox Q2 2025 Financial Results & Investor Presentation
MYSQL Presentation for SQL database connectivity
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Understanding_Digital_Forensics_Presentation.pptx
Approach and Philosophy of On baking technology
Mobile App Security Testing_ A Comprehensive Guide.pdf
sap open course for s4hana steps from ECC to s4
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Big Data Technologies - Introduction.pptx
Encapsulation_ Review paper, used for researhc scholars
Programs and apps: productivity, graphics, security and other tools
Machine learning based COVID-19 study performance prediction
Review of recent advances in non-invasive hemoglobin estimation
Encapsulation theory and applications.pdf
Teaching material agriculture food technology
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Diabetes mellitus diagnosis method based random forest with bat algorithm

computer notes - Data Structures - 27

  • 1. Class No.27 Data Structures http://ecomputernotes.com
  • 2. Priority Queue Using Heap #include “Event.cpp” #include “Heap.cpp” #define PQMAX 30 class PriorityQueue { public: PriorityQueue() { heap = new Heap<Event>( PQMAX ); }; ~PriorityQueue() { delete heap; }; http://ecomputernotes.com
  • 3. Priority Queue Using Heap Event* remove() { if( !heap->isEmpty() ) { Event* e; heap->deleteMin( e ); return e; } return (Event*)NULL; cout << &quot;remove - queue is empty.&quot; << endl; }; http://ecomputernotes.com
  • 4. Priority Queue Using Heap int insert(Event* e) { if( !heap->isFull() ) { heap->insert( e ); return 1; } cout << &quot;insert queue is full.&quot; << endl; return 0; }; int full(void){ return heap->isFull(); }; int length() { return heap->getSize(); }; }; http://ecomputernotes.com
  • 5. The Selection Problem Given a list of N elements (numbers, names etc.), which can be totally ordered, and an integer k , find the k th smallest (or largest) element. One way is to put these N elements in an array an sort it. The k th smallest of these is at the k th position. http://ecomputernotes.com
  • 6. The Selection Problem A faster way is to put the N elements into an array and apply the buildHeap algorithm on this array. Finally, we perform k deleteMin operations. The last element extracted from the heap is our answer. The interesting case is k =  N /2, since this is known as the median . http://ecomputernotes.com
  • 7. HeapSort If k = N , and we record the deleteMin elements as they come off the heap, we will have essentially sorted the N elements. Later in the course, we will refine this idea to obtain a fast sorting algorithm called heapsort . http://ecomputernotes.com
  • 8. Disjoint Set ADT Suppose we have a database of people. We want to figure out who is related to whom. Initially, we only have a list of people, and information about relations is gained by updates of the form “Haaris is related to Saad”. http://ecomputernotes.com
  • 9. Disjoint Set ADT Key property: If Haaris is related to Saad and Saad is related to Ahmad, then Haaris is related to Ahmad. Once we have relationships information, we would like to answer queries like “Is Haaris related to Ahmad?” http://ecomputernotes.com
  • 10. Disjoint Set ADT Blob Coloring A well-known low-level computer vision problem for black and white images is the following: Gather together all the picture elements (pixels) that belong to the same &quot;blobs&quot;, and give each pixel in each different blob an identical label. http://ecomputernotes.com
  • 11. Disjoint Set ADT Blob Coloring Thus in the following image, there are five blobs. We want to partition the pixels into disjoint sets , one set per blob. http://ecomputernotes.com
  • 12. Disjoint Set ADT The image segmentation problem. http://ecomputernotes.com
  • 13. Equivalence Relations A binary relation  over a set S is called an equivalence relation if it has following properties Reflexivity: for all element xS, x  x Symmetry: for all elements x and y, x  y if and only if y  x Transitivity: for all elements x, y and z, if x  y and y  z then x  z The relation “is related to” is an equivalence relation over the set of people http://ecomputernotes.com
  • 14. Equivalence Relations The  relationship is not an equivalence relation. It is reflexive, since x  x, and transitive, since x  y and y  z implies x  z, it is not symmetric since x  y does not imply y  x. http://ecomputernotes.com

Editor's Notes

  • #3: Start of lecture 33
  • #7: buildHeap is O ( N ) operation. Each deleteMin is O (log 2 N ). The total running time is thus O ( N + k log 2 N ). If k is small, or O ( N / log 2 N ), then the time required is O ( N ).
  • #14: End of lecture 33, Start of lecture 34